package com.sky.wsp.guo.mi.utils;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;
 
import java.security.Security;
 
public class SM3PasswordEncoder implements PasswordEncoder {
    private static SM3Digest digest;
    static {
        Security.addProvider(new BouncyCastleProvider());
        digest = new SM3Digest();
    }
 
    @Override
    public String encode(CharSequence rawPassword) {
        byte[] rawBytes = rawPassword.toString().getBytes();
        digest.update(rawBytes, 0, rawBytes.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return Hex.toHexString(hash); // 将字节数组转换为十六进制字符串
    }
 
    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return encode(rawPassword).equals(encodedPassword);
    }
}